import ldap
# pip install python-ldap
# https://download.lfd.uci.edu/pythonlibs/archived/python_ldap-3.4.0-cp310-cp310-win_amd64.whl


def ldap_authenticate(username, password):
    ldap_server = "ldap://192.168.3.44:30789"
    ldap_dn = f"uid={username},ou=Users,dc=kubesphere,dc=io"

    try:
        conn = ldap.initialize(ldap_server)
        conn.simple_bind_s(ldap_dn, password)
        conn.unbind_s()
        return True
    except ldap.INVALID_CREDENTIALS:
        return False
    except ldap.SERVER_DOWN:
        return False

# 调用LDAP认证函数
username = "wangwuli"
password = "Wangwuli132!"
result = ldap_authenticate(username, password)
if result:
    print("认证成功")
else:
    print("认证失败")
